package com.wzapp.sitposture.eshop.endpoint;

import com.wzapp.sitposture.common.annotation.NeedLogin;
import com.wzapp.sitposture.common.base.BaseEndpoint;
import com.wzapp.sitposture.common.base.HeaderParam;
import com.wzapp.sitposture.common.error.CustomizedException;
import com.wzapp.sitposture.common.restful.RestfulResult;
import com.wzapp.sitposture.common.util.ToolUtil;
import com.wzapp.sitposture.eshop.entity.Order;
import com.wzapp.sitposture.eshop.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping
public class OrderEndpoint extends BaseEndpoint{

    @Autowired
    OrderService orderService;

    /**
     * 功能: 买家修改订单（去支付）<br/>
     *
     * @author qianglong
     * @create 2018/1/27 14:23
     * @param id 订单id
     * @parama action_engine 操作引擎
     * @parama payMethod 支付方式
     * @parama order 订单信息
     * @return org.springframework.http.ResponseEntity
     **/
    @PatchMapping("/userCenter/userOrders/{id}/action/{action_engine}")
    @NeedLogin
    public ResponseEntity updateBuyerOrderById(@PathVariable("id") String id, @PathVariable("action_engine") String
            action_engine,@RequestBody Order order)throws CustomizedException {

        HeaderParam headerParam = super.getHeaderParam();
        String userId = headerParam.getUserId();
        String terminal = headerParam.getTerminalType();
        if(ToolUtil.isEmpty(order)){
            throw new CustomizedException("public.param.blank","order");
        }

        if ("payOrder".equalsIgnoreCase(action_engine)) {
            return RestfulResult.createSuccess(orderService.payOrder(userId, id, order.getPayMethod(), terminal));
        }else{
            return RestfulResult.createSuccess(orderService.updateOrderById(id,action_engine));
        }
    }

    /**
     * 功能: 查询用户待支付订单<br/>
     *
     * @author qianglong
     * @create 2018/1/27 18:10
     * @return org.springframework.http.ResponseEntity
     **/
    @GetMapping("/userCenter/userOrders")
    @NeedLogin
    public ResponseEntity getUserOrders() throws CustomizedException {

        String userId = super.getUserIdFromToken();

        Order order = orderService.getUserOrderByUserId(userId);
        return RestfulResult.ok(order);
    }


    /**
     * 功能: 创建订单<br/>
     *
     * @author qianglong
     * @create 2019/1/27 18:08
     * @param order
     * @return org.springframework.http.ResponseEntity
     **/
    @PostMapping(value = "/userCenter/userOrders")
    @NeedLogin
    public ResponseEntity createOrder(@RequestBody Order order) throws CustomizedException {

        order.setUserId(super.getUserIdFromToken());

        return  RestfulResult.createSuccess(orderService.createOrderInfo(order));

    }
    /**
     * 功能: 创建环信账号<br/>
     *
     * @author qianglong
     * @create 2019/10/1 0:39
     * @param
     * @return org.springframework.http.ResponseEntity
     **/
    @PostMapping("/userCenter/userOrders/action/createHuanXinByUserId")
    public ResponseEntity createHuanXinByUserId(String userId){

        if(ToolUtil.isNotEmpty(userId)){
            orderService.createHuanXinByUserId(userId);
        }

        return RestfulResult.createSuccess(null);
    }
}
